<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\common\controller\Api;
use think\Request;

class Comment extends Api
{
    protected $check = true;
    //列表接口
    public function index()
    {
        if (input('post.limit')) {
            $this->limit = input('post.limit');
        }
        $post = array_filter(input('post.'));
        if(isset($post['where']) && is_array($post['where'])){
            foreach ($post['where'] as $k=>$v){
                if($k == 'name'){
                    $this->where[] = [$k,'like','%'.$v.'%'];
                }elseif($k == 'user_index_id'){
                    if(!isset($this->userinfo) || empty($this->userinfo)){
                        $this->checkLogin();
                    }
                    $this->where[] = [$k,'=',$this->userinfo['id']];
                }else{
                    $this->where[] = [$k,'=',$v];
                }
            }
        }
        $order = $post['order'] ?? [];
        if (!empty($order)) {
            if (is_string($order)) {
                if (strstr($order, ',')) {
                    $order = explode(',', $order);
                    $arr = [];
                    foreach ($order as $k => $v) {
                        $arr[$v] = 'desc';
                    }
                    $order = $arr;
                }
            }
        } elseif(!empty($this->order)){
            $order = $this->order;
        } else {
            $order = 'id desc';
        }
        if(input('type') == 1){
            $this->where[] = ['user_index_id','=',$this->userinfo['id']];
        }else{
            $this->where[] = ['p_id','=',$this->userinfo['id']];
        }
        if ($this->select) {
            $list = $this->model->with($this->with[request()->controller()]['function'] ?? [])->order($order)->where($this->where)->select();
        } else {
            if(input('find')){
                $list = $this->model->with($this->with[request()->controller()]['function'] ?? [])->order($order)->where($this->where)->find();
            }else{

                $list = $this->model->with($this->with[request()->controller()]['function'] ?? [])->order($order)->where($this->where)->paginate($this->limit);
            }
        }
        if (!empty($list)) {
            $list = $list->toArray();
            $list = $this->index_handel($list);
            if(isset($this->with[request()->controller()]['function'])){
                if(isset($list['total'])){
                    //多条数据三数组以上，有分页
                    foreach ($this->with[request()->controller()]['function'] as $v){
                        foreach ($list['data'] as $kk=>$vv){
                            urladd($list['data'][$kk][$v]);
                        }
                    }
                }elseif(isset($list[0])){
                    //多条数据二数组以上，无分页
                    foreach ($this->with[request()->controller()]['function'] as $v){
                        foreach ($list as $kk=>$vv){
                            urladd($list[$kk][$v]);
                        }
                    }
                }else{
                    //1条数据，一维数组
                    foreach ($this->with[request()->controller()]['function'] as $v){
                        urladd($list[$v]);
                    }
                }
            }
            urladd($list);
        }
        foreach ($list['data'] as $k=>$v)
        {
            $list['data'][$k]['content'] = base64_decode($v['content']);
        }
        return datasuccess($list);
    }
    //数据添加
    public function add()
    {
        $post = array_filter(input('post.'));
        if(!empty($post)){
        	if(!isset($post['post_id']) || !isset($post['content'])) return msgerror('参数不全');
        	$pid = $post['p_id'] ?? 0;
        	$data = [
        		'post_id'=>$post['post_id'],
        		'p_id'=>$pid,
        		'user_index_id'=>$this->userinfo['id'],
        		'content'=>base64_encode($post['content'])
        		];
    		if($pid == 0){
	        	$find = $this->model->where('post_id',$post['post_id'])->order('level desc')->find();
	        	if(!empty($find) && !empty($find->toArray())){
	        		$data['level'] = $find->toArray()['level']*1+1;
	        	}else{
	        		$data['level'] = 1;
	        	}
    		}else{
    			if(!isset($post['level'])) return msgerror('参数不全');
	        	$data['level'] = $post['level'];
    		}
        	
            $this->model->save($data);
            return datasuccess();
        }else{
            return msgerror('参数缺失');
        }
    }
    
	//回复详情
	public function hf_view()
	{
		$post = array_filter(input('post.'));
		//评论数据ID
		if(!isset($post['id'])) return msgerror('参数缺失');
		$res = $this->model->where('id',$post['id'])->with('post')->find()->toArray();
		$res['content'] = base64_decode($res['content']);
		$ids = $res['user_index_id'].','.$res['p_id'];
		//所属帖子，所在楼层，评论人，回复人
		$data = $this->model->with(['user_index', 'p_user_index'])->where([['post_id','=',$res['post_id']],['level','=',$res['level']],['user_index_id','in',$ids],['p_id','in',$ids],['id','<=',$res['id']]])->order('id asc')->limit(0,2)->select()->toArray();
		foreach ($data as $k=>$v){
		    $data[$k]['content'] = base64_decode($v['content']);
		}
		$res['comment'] = $data;
		return datasuccess($res);
	}
}
